package com.wangji92.arthas.plugin.demo.common.profile;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MathGame3 {
    private static final Random random = new Random();

    private static int illegalArgumentCount = 0;

    public static void print(int number, List<Integer> primeFactors) {
        StringBuilder sb = new StringBuilder(number + "=");
        for (int factor : primeFactors) {
            sb.append(factor).append('*');
        }
        if (sb.charAt(sb.length() - 1) == '*') {
            sb.deleteCharAt(sb.length() - 1);
        }
        List<Integer> list = primeFactors(primeFactors.size());
        System.out.println("print() .sb = " + sb);
        run11(list);
    }

    private static void run11(List<Integer> list) {
        try {
            int number = random.nextInt() / 10000;
            List<Integer> primeFactors = primeFactors(number);
            System.out.println("run11 -> primeFactors = " + primeFactors);
            list.add(99999);
            run22(list);
        } catch (Exception e) {
            System.out.println(String.format("illegalArgumentCount:%3d, ", illegalArgumentCount) + e.getMessage());
        }
    }

    private static void run22(List<Integer> list) {
        System.out.println("run22 .  list = " + list);
    }

    private static List<Integer> primeFactors(int number) {
        if (number < 2) {
            illegalArgumentCount++;
        }
        List<Integer> result = new ArrayList<Integer>();
        int i = 2;
        while (i <= number) {
            if (number % i == 0) {
                result.add(i);
                number = number / i;
                i = 2;
            } else {
                i++;
            }
        }
        return result;
    }
}
